<?php

namespace app\admin\controller\subject;

use app\common\controller\Backend;

class Order extends Backend
{
    // 设置关联查询
    protected $relationSearch = true;

    // 当前模型
    protected $model = null;

    // 无需登陆
    protected $noNeedLogin = [];
    // 无需鉴权
    protected $noNeedRight = [];


    public function __construct(){
        parent::__construct();
        $this->model = model('Subject.Order');
        $this->BusinessModel = model('Business.User');
        $this->SubjectModel = model('Subject.Subject');
    }

    public function index(){
        // 接收数据,去除html标签和空白
        $this->request->filter(['strip_tags','trim']);

        if($this->request->isAjax()){
            list($where,$sort,$order,$offset,$limit) = $this->buildparams();

            // 表格需要两个返回值 总条数和分页的数据
            $total = $this->model
                ->with(['subject','business'])
                ->where($where)
                ->order($sort,$order)
                ->limit($offset,$limit)
                ->count();

            $list = $this->model
                ->with(['subject','business'])
                ->where($where)
                ->order($sort,$order)
                ->limit($offset,$limit)
                ->select();

            $result = [
                'total' => $total,
                'rows' => $list
            ];
            
            return json($result);
            
        }
        return $this->view->fetch();
    }

    public function del($ids=NULL){
        $list = $this->model->select($ids);
        // var_dump($list);
        // exit;

        // 没有数据
        if(!$list){
            $this->error(__('No Results were found'));
            exit;
        }

        // 软删除
        $result = $this->model->destroy($ids);
        
        if($result === FALSE){
            $this->error("删除失败");
            exit;
        }else{
            $this->success("删除成功");
            exit;
        }
    }
}
